package com.coders.android.hibernate.dao.impl;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.coders.android.hibernate.dao.UsuarioDAO;
import com.coders.android.hibernate.dto.Usuario;

public class UsuarioDAOHibernate extends HibernateDaoSupport implements UsuarioDAO {

	@Override
	public void insertar(Usuario usuario) {

		Transaction transaction = null;
		try{
			Session sesion = getSession();
			
			transaction = sesion.beginTransaction();
			sesion.save( usuario );
			transaction.commit();
			
		}catch (HibernateException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void eliminar(String imei) {

		Transaction transaction = null;
		try{
			Session sesion = getSession();
			
			transaction = sesion.beginTransaction();
			
			String hqlQuery = ""
					+ "DELETE FROM Usuario "
					+ "WHERE imei = :imei";
			
			sesion.createQuery( hqlQuery ).setString( "imei", imei ).executeUpdate();
			
			transaction.commit();
			
		}catch (HibernateException e) {
			e.printStackTrace();
		}
	}
}
